home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / mscfunct / crt_line.c < prev    next >
Text File  |  1987-05-11  |  903b  |  36 lines

  1. /*    draw a line in graphics mode on color monitor
  2. */
  3.  
  4. /* simple sign function */
  5. #define sign(a) ( (a < 0) ? -1.0 : 1.0 )
  6.  
  7. void crt_line(x1,y1,x2,y2,color) int x1, y1, x2, y2, color;
  8. {
  9.  
  10.     float x, y, xinc, yinc;
  11.     int xp, yp;
  12.  
  13.     if( x1 == x2 ){                /* delta x = 0 ? */
  14.     xinc = 0.; yinc = sign( y2-y1 );
  15.     }
  16.     else if( y1 == y2 ){            /* delta y = 0 ? */
  17.         yinc = 0.; xinc = sign( x2-x1 );
  18.     }
  19.     else {            /* assume delta x > delta y */
  20.     xinc = 1.;
  21.     yinc = ( y2 - y1 ) / (float)abs( x2 - x1 );
  22.     if( abs( y1 - y2 ) > abs( x1 - x2 ) ){    /* check */
  23.         yinc = 1.;
  24.         xinc = ( x2 - x1 ) / (float)abs( y2 - y1 );
  25.     }
  26.     }
  27.     
  28.     /* loop, incrementing x & y until end point is reached */
  29.     for( x = xp = x1, y = yp = y1; !( xp == x2 && yp == y2 );
  30.     x += xinc, y += yinc, xp = x + 0.5, yp = y + 0.5 )
  31.         crt_write_pixel( xp, yp, color );
  32. }
  33.     
  34.     
  35.     
  36.